home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 June / MacFormat 25.iso / Shareware City / Developers / metro-c-cpp-121 / C_C++_1.2.1 / READMEs / MW Templates < prev    next >
Encoding:
Text File  |  1995-01-19  |  2.6 KB  |  98 lines  |  [TEXT/MMCC]

  1. ===================================
  2. Tech Note: Metrowerks C++ Templates
  3. ===================================
  4.  
  5. Instantiation:
  6. ==============
  7.  
  8. MW C++ supports (semi-)automatic and explicit template instantiation:
  9.  
  10. --File:templ.h-------------
  11.     //    template declarations
  12.     
  13.     template <class T> class Templ {
  14.         T    member;
  15.     public:
  16.             Templ(T x) { member=x; }
  17.         T    Get();
  18.     };
  19.     
  20.     template <class T> T Max(T,T);
  21. ---------------------------
  22.  
  23. --File:templ.cp------------
  24.     //    template function definitions
  25.     
  26.     #include "templ.h"
  27.     
  28.     template <class T> T Templ<T>::Get() { return member; }
  29.     
  30.     template <class T> T Max(T x,T y) { return (x>y)?x:y; }
  31. ---------------------------
  32.  
  33. Automatic instantiation:
  34. ========================
  35.  
  36. For automatic instantiation all used template definitions have to be available
  37. at the end of the translation unit. All required template function instances will
  38. then be automatically generated:
  39.  
  40. --File:auto.cp-------------
  41.     #include "templ.cp"                    //    include template declarations & defintions
  42.  
  43.     long foomax(Templ<long> a,Templ<long> b)
  44.     {
  45.         return Max(a.Get(),b.Get());
  46.     }
  47.     //    The template functions:    "long Max<long>(long,long);" and
  48.     //    "long Templ<long>::Get();" will be created as automatic instances
  49. ---------------------------
  50.  
  51. Explicit instantiation:
  52. =======================
  53.  
  54. MW C++ supports the ANSI C++ syntax and semantics for explicit instantiation:
  55.  
  56.     explicit-intantiation:
  57.         template <inst> ;
  58.  
  59.     inst:
  60.         <class-key> <template-id>
  61.         <type-specifier-seq> <template-id> ( <parameter-declaration-clause> )
  62.  
  63. For example:
  64.     template class Templ<int>;
  65.     template int Max<int>(int,int);
  66.  
  67. A declaration of the template must be in scope and the definition of the template
  68. must be available at the point of explicit instantiation. Explicit instantiation
  69. of a template class implies the instantiation of all its members not previously
  70. explicitly specialized.
  71.  
  72. --File:expl.cp-------------
  73.     #include "templ.h"                    //    include template declarations only
  74.  
  75.     long foomax(Templ<long> a,Templ<long> b)
  76.     {
  77.         return Max(a.Get(),b.Get());
  78.     }
  79.     //    no automatic instances will be created
  80. ---------------------------
  81.  
  82. --File:inst.cp-------------
  83.     #include "templ.cp"                    //    include template declarations & defintions
  84.  
  85.     template class Templ<long>;            //    explicit class instantiation
  86.     //    will create the member function instance "long Templ<long>::Get();"
  87.  
  88.     template long Max<long>(long,long);    //    explicit function instantiation
  89.     //    will create the function instance "long Max<long>(long,long);"
  90. ---------------------------
  91.  
  92. Explicit and automatic instantiation can be freely mixed.
  93. An explict instantiation will override any automatic instances.
  94.  
  95. ====================================
  96.  
  97. Features that are not yet supported:
  98.